﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.admin.service;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// rm_base 的摘要说明
    /// </summary>
    public class rm_base : IHttpHandler
    {
        
        private OperateLogService s = new OperateLogService();
        
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];
            string url = context.Request.Url.PathAndQuery;
            string pageName = getPageName(url);
            UserEntity user = UserService.GetUser(context);
            if (user == null) return;
            if (!new MenuService().ifAuthorize(user.id, pageName, action)) {
                string userIp = context.Request.UserHostAddress;
                string details = context.Request.Url.PathAndQuery;
                StringBuilder sb = new StringBuilder();
                sb.Append(details);
                sb.AppendFormat("{0}未授权当前操作{1}的{2}",user.userName,pageName,action);
                s.add(user.userName, userIp, action, false, sb.ToString());

                context.Response.Write(JsonUtil.ToJson(new { success = false, 
                    msg = string.Format("{0},您未授权进行当前操作，请与管理员联系！",user.userName)}));
                return;
            }
            try
            {
                switch (action)
                {
                    case "getall":
                        getall(context);
                        break;
                    case "get":
                        get(context);
                        break;
                    case "search":
                        search(context);
                        break;
                    case "add":
                        add(context);
                        break;
                    case "edit":
                        edit(context);
                        break;
                    case "delete":
                        delete(context);
                        break;
                    default:
                        other(context);
                        break;
                }                
                
                string userIp = context.Request.UserHostAddress;
                string details = context.Request.Url.PathAndQuery;
                StringBuilder sb = new StringBuilder();
                sb.Append(details);
                if (!action.StartsWith("get") && !action.StartsWith("search"))
                {
                    s.add(user.userName, userIp, action, true, sb.ToString());
                }
            }
            catch (Exception ex)
            {                
                string userIp = context.Request.UserHostAddress;
                string details = context.Request.Url.PathAndQuery;
                StringBuilder sb = new StringBuilder();
                sb.Append(details);
                sb.Append("exception："+ex.Message);
                s.add(user.userName, userIp, action, false, sb.ToString());

                context.Response.Write(JsonUtil.ToJson(new { success = false, msg = ex.Message }));
            }
        }
        private string getPageName(string url) {
            int x1 = url.IndexOf("rm_");
            
            int x2 = url.IndexOf(".ashx");
            if (x1 >= 0 && x2>x1) {
                return url.Substring(x1 + 3, x2 - x1 - 3);
            }
            x1 = url.IndexOf("bg_");
            if (x1 >= 0 && x2>x1) {
                return url.Substring(x1 + 3, x2 - x1 - 3);
            }
            return "";
        }
        public virtual void getall(HttpContext context) {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void get(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void search(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void add(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void edit(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void delete(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public virtual void other(HttpContext context)
        {
            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "功能未实现！" }));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}